/**
 * 
 */
package example.topic.services.impl;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.vanstone.common.ObjectDuplicateException;

import example.business.api.id.services.IDService;
import example.business.api.topic.Tag;
import example.business.api.topic.services.TagService;
import example.common.id.SystemIDSchema;
import example.topic.dal.object.TopicTagDO;

/**
 * @author shipeng
 *
 */
@Service("tagService")
public class TagServiceImpl implements TagService {

  @Autowired
  private IDService idService;
  @Autowired
  private MongoTemplate mongoTemplate;

  @Override
  public Tag addTag(String tagname) throws ObjectDuplicateException {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public Tag saveTag(String tagname) {
    TopicTagDO topicTagDO = this.mongoTemplate.findOne(Query.query(Criteria.where("tagname").is(tagname)), TopicTagDO.class);
    if (topicTagDO != null) {
      return BeanUtil.toTag(topicTagDO);
    }
    Tag tag = new Tag();
    tag.setId(idService.nextID(SystemIDSchema.Topic_Tag));
    tag.setTagname(tagname);
    this.mongoTemplate.insert(BeanUtil.toTopicTag(tag));
    return tag;
  }

  @Override
  public Tag getTag(long id) {
    TopicTagDO topicTagDO = this.mongoTemplate.findById(id, TopicTagDO.class);
    if (topicTagDO == null) {
      return null;
    }
    return BeanUtil.toTag(topicTagDO);
  }

  @Override
  public Map<Long, Tag> getTagsAsMap(List<Long> ids) {
    Map<Long, Tag> map = new HashMap<>();
    if (ids == null) {
      return map;
    }
    Criteria criteria = new Criteria("_id");
    criteria.in(ids);
    Query query = new Query(criteria);
    List<TopicTagDO> topicTagDOs = this.mongoTemplate.find(query, TopicTagDO.class);
    if (topicTagDOs == null || topicTagDOs.size() <= 0) {
      return map;
    }
    for (TopicTagDO topicTagDO : topicTagDOs) {
      Tag tag = BeanUtil.toTag(topicTagDO);
      map.put(tag.getId(), tag);
    }
    return map;
  }

  @Override
  public List<Tag> getTags(List<Long> ids) {
    List<Tag> tags = new ArrayList<>();
    if (ids == null) {
      return tags;
    }
    Criteria criteria = new Criteria("_id");
    criteria.in(ids);
    Query query = new Query(criteria);
    List<TopicTagDO> topicTagDOs = this.mongoTemplate.find(query, TopicTagDO.class);
    if (topicTagDOs == null || topicTagDOs.size() <= 0) {
      return tags;
    }
    for (TopicTagDO topicTagDO : topicTagDOs) {
      Tag tag = BeanUtil.toTag(topicTagDO);
      tags.add(tag);
    }
    return tags;
  }

  @Override
  public Tag getTagByName(String tagname) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public Tag updateTaginfo(long id, String tagname) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public void deleteTag(long id) {
    // TODO Auto-generated method stub

  }

  @Override
  public Tag getTags(long offset, long limit) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public long getTotalTags() {
    // TODO Auto-generated method stub
    return 0;
  }

}
